/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.market.web.EJB;

import com.market.web.model.Customer;
import com.market.web.model.Item;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;

/*
 *
 * @author ps
*/
@Stateless
public class AllItems {
    
    List<Item> lstItem;
    private Customer customer;
    double total;
    
    public AllItems() {
        lstItem = new ArrayList<Item>();
        customer = null;
        total = 0;
    }

    public void addItem(Item i) {

        boolean newItem = true;

        for (Item item : lstItem) {

            if (item.getId() == i.getId()) {

                newItem = false;
                item.changeQty(i.getQty());
            }
        }

        if (newItem) {
            Item item = new Item(i.getId(),i.getName(),i.getPrice(),i.getQty());
            lstItem.add(item);
        }
    }
    public List<Item> getItems() {
        return lstItem;
    }
    
    public double getTotal() {
        double amount =0.0;
        
        for (Item i : lstItem) {

            amount += i.getSubTotal();
            
        }

        return amount;
    }
    public void emptyOne(int id)
    {
        for(Item i:lstItem)
        {
            if(i.getId()==id)
            {
                lstItem.remove(i);
                break;
            }
        }
    }
    
     public void emptyCart() {
        lstItem.clear();
        total = 0;
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
}
